package com.srxz.personnelsystem.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: PageParam
 * @author: wgchao
 * @createTime: 2019/1/23 11:13 AM
 * @Description: TODO
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageParam implements Serializable {
    private int offset;//页码数
    private int limit;//单页条数
    private int secho;//未知作用
    private HashMap<String, String> orders;//排序规则
    private HashMap<String, Object> searchCon;//查询条件

    //查询后
    private long total;//数据总数
    private int pagecount;//总页数
    private Object rows;//数据列表

    public int getCurrPage(){
        return (this.offset+this.limit-1)/this.limit;
    }

    public Sort getSort(){
        if(this.orders!=null && !this.orders.isEmpty()){
            List<Sort.Order> ods = new ArrayList<>();
            for(Map.Entry<String, String> ent:this.orders.entrySet()){
                if(StringUtils.isEmpty(ent.getValue()) || ent.getValue().toLowerCase().equals("asc")){
                    ods.add(Sort.Order.asc(ent.getKey()));
                }
                else{
                    ods.add(Sort.Order.desc(ent.getKey()));
                }
            }
            return Sort.by(ods);
        }
        return null;
    }

}
